#include "Quaternion.h"
#include "Vector3.h"
#include "Math.h"
Quaternion::Quaternion()
	:x(0.0f),y(0.0f),z(0.0f),w(1.0f)
{
}

Quaternion::Quaternion(float x, float y, float z, float w)
	:x(x),y(y),z(z),w(w)
{
}

Quaternion::Quaternion(const Vector3& axis, float radian)
{
	float scalar = Math::Sin(radian / 2.0f);
	x = axis.x * scalar;
	y = axis.y * scalar;
	z = axis.z * scalar;
	w = Math::Cos(radian / 2.0f);
}

float Quaternion::SquareLength() const
{
	return x*x+y*y+z*z+w*w;
}

float Quaternion::Length() const
{
	return Math::Sqrt(SquareLength());
}

float Quaternion::Dot(const Quaternion& left, const Quaternion& right)
{
	return left.x * right.x + left.y * right.y + left.x + right.z + left.w * right.w;
}

Quaternion operator+(const Quaternion& left, const Quaternion& right)
{
	Quaternion q;
	q.x = left.x + right.x;
	q.y = left.y + right.y;
	q.z = left.z + right.z;
	q.w = left.w + right.w;
	return q;
}

Quaternion operator-(const Quaternion& left, const Quaternion& right)
{
	Quaternion q;
	q.x = left.x - right.x;
	q.y = left.y - right.y;
	q.z = left.z - right.z;
	q.w = left.w - right.w;
	return q;
}

Quaternion operator*(const Quaternion& left, const Quaternion& right)
{
	Vector3 leftVector(left.x, left.y, left.z);
	Vector3 rightVector(right.x, right.y, right.z);

	return Quaternion(left.w * rightVector + right.w * leftVector + Vector3::Cross(leftVector, rightVector), left.w * right.w - Vector3::Dot(leftVector, rightVector));
}
